package service

import (
	"blog_server/internal/model/entity"
	"blog_server/internal/model/request"
	"blog_server/pkg/app"
)

func (svc *Service) GetAllCategory() ([]entity.Category, error) {
	return svc.dao.GetAllCategory()
}

func (svc *Service) CreateCategory(category *entity.Category) error {
	return svc.dao.CreateCategory(category)
}

func (svc Service) GetCategoriesByPagination(page, pageSize int) ([]entity.Category, int, error) {
	list, err := svc.dao.GetCategoriesByPagination(app.GetPageOffset(page, pageSize), pageSize)
	if err != nil {
		return nil, 0, err
	}
	cnt, err := svc.dao.CountCategories()
	return list, cnt, err
}

func (svc Service) UpdateCategory(id uint, param *request.CategoryRequest) error {
	return svc.dao.UpdateCategory(entity.Category{
		ID:            id,
		CategoryName:  param.CategoryName,
		CategoryDes:   param.CategoryDes,
		CoverImageUrl: param.CoverImageUrl,
	})
}

func (svc Service) DeleteCategory(id uint) error {
	return svc.dao.DeleteCategory(entity.Category{ID: id})
}

func (svc Service) GetCategoriesBySearchText(text string) ([]entity.Category, error) {
	return svc.dao.GetCategoriesBySearchText(text)
}
